package com.bawei.mall.cart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.mall.cart.domain.MallProductCart;
import com.bawei.mall.cart.domain.reponse.MallProductCartVo;
import com.bawei.mall.cart.mapper.MallProductCartMapper;
import com.bawei.mall.cart.service.MallProductCartService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：作者姓名
 * @Package：com.bawei.mall.cart.service.impl
 * @Project：mall_cloud
 * @name：MallProductCartServiceImpl
 * @Date：2024/7/5 15:00
 */
@Service
public class MallProductCartServiceImpl extends ServiceImpl<MallProductCartMapper, MallProductCart> implements MallProductCartService{

    @Autowired
    private MallProductCartMapper mallProductCartMapper;


    @Override
    public List<MallProductCartVo> selectmallProductCartList() {
        /*LambdaQueryWrapper<MallProductCart> wrapper = new LambdaQueryWrapper<>();
        Page<MallProductCart> mallProductCartPage = new Page<>(1, 10);
        List<MallProductCart> records = this.baseMapper.selectPage(mallProductCartPage, wrapper)
                .getRecords();*/
        List<MallProductCartVo> mallProductCartVos = mallProductCartMapper.selectmallProductCartList();
        mallProductCartVos.forEach(skuInfo -> {
            String input = skuInfo.getProductAttr();
            List<String> properties = splitProperties(input);
            String json = convertToJson(properties);
            skuInfo.setProductAttr(json);
        });
        return mallProductCartVos;
    }

    @Override
    public void mallProductCartClear() {
        mallProductCartMapper.delete(null);
    }

    private static List<String> splitProperties(String input) {
        String[] parts = input.split(",");
        List<String> properties = new ArrayList<>();
        for (String part : parts) {
            properties.add(part.trim());
        }
        return properties;
    }
    private static String convertToJson(List<String> properties) {
        Map<String, StringBuilder> propertyMap = new HashMap<>();

        // Populate property map
        for (String property : properties) {
            if (property.contains("色")) {
                String[] keyValue = property.split("色");
                if (keyValue.length == 2) {
                    if (!propertyMap.containsKey("颜色")) {
                        propertyMap.put("颜色", new StringBuilder(keyValue[1]));
                    } else {
                        propertyMap.get("颜色").append("+").append(keyValue[1]);
                    }
                } else {
                    if (!propertyMap.containsKey("颜色")) {
                        propertyMap.put("颜色", new StringBuilder(property));
                    } else {
                        propertyMap.get("颜色").append("+").append(property);
                    }
                }
            } else {
                if (!propertyMap.containsKey("容量")) {
                    propertyMap.put("容量", new StringBuilder(property));
                } else {
                    propertyMap.get("容量").append("+").append(property);
                }
            }
        }

        // Convert property map to list of Property objects
        List<Property> jsonObjects = new ArrayList<>();
        propertyMap.forEach((key, value) -> {
            jsonObjects.add(new Property(key, value.toString()));
        });

        // Convert to JSON
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(jsonObjects);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "";
        }
    }

    // Simple Property class for storing key-value pairs
    static class Property {
        private String key;
        private String value;

        public Property(String key, String value) {
            this.key = key;
            this.value = value;
        }

        // Getters and Setters
        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }
}
